package com.atguigu.msm.controller;

import com.atguigu.utils.R;
import com.atguigu.msm.service.MsmService;
import com.atguigu.serviceBash.GlobalException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author zy
 */
@RestController
@CrossOrigin
@Api(value = "MsmController", description = "验证码接口 8005")
@RequestMapping("/msm/service")
public class MsmController {

    @Autowired
    MsmService msmService;

    @ApiOperation("发送验证码接口")
    @PostMapping("/send/{phoneNumber}")
    public R sendMessage(@PathVariable String phoneNumber) {
        try {
            long l = Long.parseLong(phoneNumber);
        } catch (Exception e) {
            throw new GlobalException("请输入正确的手机号", 20001);
        }
        if (StringUtils.isEmpty(phoneNumber)) {
            throw new GlobalException("手机号不能为空", 20001);
        }

        if (phoneNumber.length() != 11) {
            throw new GlobalException("请输入正确的手机号", 20001);
        }
        //   如果存在 不发送 直接返回
        String code = msmService.sendMessage(phoneNumber);
        return R.ok().data("code", code);
    }
}
